package com.fengkong.analysis.simple;

import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式测试
 *
 * @Author longdeng
 * @Date 2020/7/29 0029 0:44
 */
public class RegexTest {

    /**
     * 测试正则表达式-断言3
     */
    @Test
    public void regex5Test() {
        // 有点理解了，但是不是很理解
        String data = "#ab#c#";
        String patternStr = "#([\\w\\W]+?)#";
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(data);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }

    /**
     * 测试正则表达式-断言3
     */
    @Test
    public void regex4Test() {
        // 有点理解了，但是不是很理解
        String data = "#a##b##c#";
        String patternStr = "(?<=#)([\\w\\W]{1,}?)(?>=#)";
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(data);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }

    /**
     * 测试正则表达式-断言2
     */
    @Test
    public void regex3Test() {

        String data = "abbbc|abbbc";
        String patternStr = "(?<=a)bbb(?=c)";
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(data);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }

    /**
     * 测试正则表达式-断言
     */
    @Test
    public void regex2Test() {

        String data = "#a##b##c#";
        String patternStr = "(#[\\w\\W]{1,}?#)";
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(data);
        while (matcher.find()) {
            String group = matcher.group();

            String p2 = "(?<=#)([\\w\\W]{1,})(?=#)";
            Pattern pattern2 = Pattern.compile(p2);
            Matcher matcher2 = pattern2.matcher(group);

            while (matcher2.find()) {
                System.out.println(matcher2.group());
            }

        }
    }

    /**
     * 测试正则表达式
     */
    @Test
    public void regexTest() {

        String data = "#a##b##c#";
        String patternStr = "(#[\\w\\W]{1,}?#)";
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(data);

        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }

}
